package com.chuangke.flow.dao.mapper;

import java.util.Collection;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangke.flow.entity.FlowHiFlow;

/**
 * 流程Mapper接口
 * 
 * @author chuangke
 * @date "2020-09-27"
 */
@Mapper
public interface FlowHiFlowMapper extends BaseMapper<FlowHiFlow> {

	@Select("select * from flow_hi_flow where id in (select flow_id from flow_hi_task_log where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status in (#{agreeStatus},#{rejectStatus})) order by order_created_at desc")
	IPage<FlowHiFlow> selectDone(IPage<FlowHiFlow> page,@Param("userId") String userId,@Param("agreeStatus") String agreeStatus,@Param("rejectStatus") String rejectStatus);

	@Select("select b.name as task_name,\n" +
			"b.remarks as task_remark,\n" +
			"b.tId as tId,a.* from flow_hi_flow a left join (\n" +
			"select\n" +
			"id,\n" +
			"name,\n" +
			"remarks,\n" +
			"'mds_kb_knowledge' as tId\n" +
			"from\n" +
			"mds_kb_knowledge\n" +
			"union all\n" +
			"select\n" +
			"id,\n" +
			"name ,\n" +
			"remarks,\n" +
			"'mds_technology_process' as tId\n" +
			"from\n" +
			"mds_technology_process\n" +
			"union all\n" +
			"select\n" +
			"id,\n" +
			"name,\n" +
			"remarks,\n" +
			"'mds_material' as tId\n" +
			"from\n" +
			"mds_material) b on a.order_id = b.id" +
			" where a.id in (select flow_id from flow_hi_task_log " +
			"where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status in (#{agreeStatus},#{rejectStatus})) order by order_created_at desc")
	IPage<FlowHiFlow> selectDoneWithName(IPage<FlowHiFlow> page,@Param("userId") String userId,@Param("agreeStatus") String agreeStatus,@Param("rejectStatus") String rejectStatus);

	@Select("select count(1) from flow_hi_flow where id in (select flow_id from flow_hi_task_log where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status in (#{agreeStatus},#{rejectStatus})) order by order_created_at desc")
	int selectDoneCount(@Param("userId") String userId,@Param("agreeStatus") String agreeStatus,@Param("rejectStatus") String rejectStatus);
	
	@Select("select * from flow_hi_flow where order_status=#{orderStatus} and id in (select flow_id from flow_hi_task_log where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status in (#{agreeStatus},#{rejectStatus})) order by order_created_at desc")
	IPage<FlowHiFlow> selectDownAndOver(IPage<FlowHiFlow> page,@Param("userId") String userId,@Param("orderStatus") String orderStatus,@Param("agreeStatus") String agreeStatus,@Param("rejectStatus") String rejectStatus);
	@Select("select a.*,b.name as task_name,b.remarks as task_remark,b.tId as tId from flow_hi_flow a left join (\n" +
			"select\n" +
			"id,\n" +
			"name,\n" +
			"remarks,\n" +
			"'mds_kb_knowledge' as tId\n" +
			"from\n" +
			"mds_kb_knowledge\n" +
			"union all\n" +
			"select\n" +
			"id,\n" +
			"name ,\n" +
			"remarks,\n" +
			"'mds_technology_process' as tId\n" +
			"from\n" +
			"mds_technology_process\n" +
			"union all\n" +
			"select\n" +
			"id,\n" +
			"name,\n" +
			"remarks,\n" +
			"'mds_material' as tId\n" +
			"from\n" +
			"mds_material) b on a.order_id = b.id where a.order_status=#{orderStatus} and a.id in (select flow_id from flow_hi_task_log where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status in (#{agreeStatus},#{rejectStatus})) order by order_created_at desc")
	IPage<FlowHiFlow> selectDownAndOverWithName(IPage<FlowHiFlow> page,@Param("userId") String userId,@Param("orderStatus") String orderStatus,@Param("agreeStatus") String agreeStatus,@Param("rejectStatus") String rejectStatus);

	@Select("select count(1) from flow_hi_flow where order_status=#{orderStatus} and id in (select flow_id from flow_hi_task_log where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status in (#{agreeStatus},#{rejectStatus})) order by order_created_at desc")
	int selectDownAndOverCount(@Param("userId") String userId,@Param("orderStatus") String orderStatus,@Param("agreeStatus") String agreeStatus,@Param("rejectStatus") String rejectStatus);
	
	@Select("select * from flow_hi_flow where id in (select flow_id from flow_hi_task_log where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status = #{rejectStatus}) order by order_created_at desc")
	IPage<FlowHiFlow> selectReject(IPage<FlowHiFlow> page,@Param("userId") String userId,@Param("rejectStatus") String rejectStatus);
	@Select("select a.*,b.name as task_name,b.remarks as task_remark,b.tId as tId from flow_hi_flow a left join (\n" +
			"select\n" +
			"id,\n" +
			"name,\n" +
			"remarks,\n" +
			"'mds_kb_knowledge' as tId\n" +
			"from\n" +
			"mds_kb_knowledge\n" +
			"union all\n" +
			"select\n" +
			"id,\n" +
			"name ,\n" +
			"remarks,\n" +
			"'mds_technology_process' as tId\n" +
			"from\n" +
			"mds_technology_process\n" +
			"union all\n" +
			"select\n" +
			"id,\n" +
			"name,\n" +
			"remarks,\n" +
			"'mds_material' as tId\n" +
			"from\n" +
			"mds_material) b on a.order_id = b.id" +
			" where a.id in (select flow_id from flow_hi_task_log where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status = #{rejectStatus}) order by order_created_at desc")
	IPage<FlowHiFlow> selectRejectWithName(IPage<FlowHiFlow> page,@Param("userId") String userId,@Param("rejectStatus") String rejectStatus);

	@Select("select count(1) from flow_hi_flow where id in (select flow_id from flow_hi_task_log where flow_hi_task_log.user_id=#{userId} and flow_hi_task_log.status = #{rejectStatus}) order by order_created_at desc")
	int selectRejectCount(@Param("userId") String userId,@Param("rejectStatus") String rejectStatus);
	@Select("select a.*,b.name as task_name,b.remarks as task_remark,b.tId as tId from flow_hi_flow a " +
			" left join ("+
			"select id,name,remarks,'mds_kb_knowledge' as tId from mds_kb_knowledge \n" +
			"union all\n" +
			"select id,name ,remarks,'mds_technology_process' as tId from mds_technology_process \n" +
			"union all\n" +
			"select id,name,remarks,'mds_material' as tId from mds_material) b"+
			" on a.order_id = b.id " +
			" where a.order_created_by=#{userId}  order by a.created_at desc")
	IPage<FlowHiFlow> selectFlowAndMyTaskName(IPage<FlowHiFlow> page, @Param("userId") String userId);
	
	/**
     * 批量插入 仅适用于mysql
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<FlowHiFlow> entityList);
}
